<template>
  <div style="padding: 8px">
    <el-row :gutter="16">
      <el-col :span="24">
        <div class="ptitle">
          {{ yearName }}{{ $t("年") }}{{ $t("供应商年审") }}
        </div>

        <div class="learun-form-title mt-16">{{ $t("基本信息") }}</div>

        <div class="form_block flex flex-center flex-c-between">
          <div>
            {{ $t("供应商编号：" + formData.case_erp_supplier_f_Number) }}
          </div>
          <div>
            {{ $t("供应商名称：" + formData.case_erp_supplier_f_Name) }}
          </div>
          <div>
            {{ $t("供应商负责人：" + formData.case_erp_supplier_f_Person) }}
          </div>
          <div>
            {{ $t("供应商类型：" + formData.case_erp_supplier_f_Type) }}
          </div>
          <div>
            {{ $t("添加人：" + formData.case_erp_supplier_f_CreateUserName) }}
          </div>
          <div>
            {{ $t("创建日期：" + formData.case_erp_supplier_f_CreateDate) }}
          </div>
          <div>
            {{ $t("经营范围：" + formData.case_erp_supplier_f_Scope) }}
          </div>
          <div>
            {{ $t("手机号：" + formData.case_erp_supplier_f_Phone) }}
          </div>
        </div>
      </el-col>
      <div style="padding: 0 8px">
        <el-form
          class="learun-el-form-left"
          :model="yearFormData"
          :rules="isReadOnly ? {} : rules"
          size="mini"
          labelPosition="left"
          labelWidth="100px"
          ref="form"
        >
          <el-row class="erp-supplier-assess">
            <el-col :span="24">
              <div class="learun-form-title mt-16">
                {{ $t("供货能力评估") }}
              </div>
              <div class="form_block">
                <el-form-item
                  :label="$t('等级：')"
                  prop="case_erp_supplierrisk_f_CapacityLevel"
                  v-if="
                    !lr_formEditAuth(
                      'case_erp_supplierrisk_f_CapacityLevel',
                      false || isReadOnly
                    )
                  "
                >
                  <el-tag
                    v-if="
                      yearFormData.case_erp_supplierrisk_f_CapacityLevel == '0'
                    "
                    >{{ $t("优秀") }}</el-tag
                  >
                  <el-tag
                    v-else-if="
                      yearFormData.case_erp_supplierrisk_f_CapacityLevel == '1'
                    "
                    type="success"
                    >{{ $t("良好") }}</el-tag
                  >
                  <el-tag
                    v-else-if="
                      yearFormData.case_erp_supplierrisk_f_CapacityLevel == '2'
                    "
                    type="info"
                    >{{ $t("及格") }}</el-tag
                  >
                  <el-tag v-else type="danger">{{ $t("不及格") }}</el-tag>
                </el-form-item>
                <el-form-item
                  :label="$t('等级选择：')"
                  v-else
                  prop="case_erp_supplierrisk_f_CapacityLevel"
                >
                  <l-radio
                    :options="levelOptions"
                    v-model="yearFormData.case_erp_supplierrisk_f_CapacityLevel"
                  />
                </el-form-item>

                <el-form-item
                  :label="$t('评估理由：')"
                  prop="case_erp_supplierrisk_f_CapacityReason"
                >
                  <div v-if="isReadOnly">
                    {{ yearFormData.case_erp_supplierrisk_f_CapacityReason }}
                  </div>
                  <el-input
                    v-else
                    type="textarea"
                    style="width: 500px"
                    :disabled="
                      !lr_formEditAuth(
                        'case_erp_supplierrisk_f_CapacityReason',
                        isReadOnly
                      )
                    "
                    :showWordLimit="false"
                    v-model="
                      yearFormData.case_erp_supplierrisk_f_CapacityReason
                    "
                    :placeholder="$t('请输入')"
                  />
                </el-form-item>
                <el-form-item
                  :label="$t('附件上传：')"
                  prop="case_erp_supplierrisk_f_CapacityFile"
                >
                  <el-col
                    v-if="
                      lr_formLookAuth('case_erp_supplierrisk_f_CapacityFile')
                    "
                    :span="24"
                  >
                    <el-form-item
                      prop="case_erp_supplierrisk_f_CapacityFile"
                      labelWidth="0"
                    >
                      <l-upload
                        drag
                        maxSize="10"
                        sizeType="MB"
                        :limit="3"
                        showFileList="uploadTheme1"
                        :disabled="
                          !lr_formEditAuth(
                            'case_erp_supplierrisk_f_CapacityFile',
                            false || isReadOnly
                          )
                        "
                        v-model="
                          yearFormData.case_erp_supplierrisk_f_CapacityFile
                        "
                      />
                    </el-form-item>
                  </el-col>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="24">
              <div class="learun-form-title mt-16">
                {{ $t("供货质量评估") }}
              </div>

              <div class="form_block">
                <el-form-item
                  :label="$t('等级：')"
                  prop="case_erp_supplierrisk_f_SupplierLevel"
                  v-if="
                    !lr_formEditAuth(
                      'case_erp_supplierrisk_f_SupplierLevel',
                      false || isReadOnly
                    )
                  "
                >
                  <el-tag
                    v-if="
                      yearFormData.case_erp_supplierrisk_f_SupplierLevel == '0'
                    "
                    >{{ $t("优秀") }}</el-tag
                  >
                  <el-tag
                    v-else-if="
                      yearFormData.case_erp_supplierrisk_f_SupplierLevel == '1'
                    "
                    type="success"
                    >{{ $t("良好") }}</el-tag
                  >
                  <el-tag
                    v-else-if="
                      yearFormData.case_erp_supplierrisk_f_SupplierLevel == '2'
                    "
                    type="info"
                    >{{ $t("及格") }}</el-tag
                  >
                  <el-tag v-else type="danger">{{ $t("不及格") }}</el-tag>
                </el-form-item>
                <el-form-item
                  :label="$t('等级选择：')"
                  v-else
                  prop="case_erp_supplierrisk_f_SupplierLevel"
                >
                  <l-radio
                    :options="levelOptions"
                    v-model="yearFormData.case_erp_supplierrisk_f_SupplierLevel"
                  />
                </el-form-item>
                <el-form-item
                  :label="$t('评估理由：')"
                  prop="case_erp_supplierrisk_f_SupplierReason"
                >
                  <div v-if="isReadOnly">
                    {{ yearFormData.case_erp_supplierrisk_f_SupplierReason }}
                  </div>
                  <el-input
                    v-else
                    type="textarea"
                    style="width: 500px"
                    :disabled="
                      !lr_formEditAuth(
                        'case_erp_supplierrisk_f_SupplierReason',
                        isReadOnly
                      )
                    "
                    v-model="
                      yearFormData.case_erp_supplierrisk_f_SupplierReason
                    "
                    :placeholder="$t('请输入')"
                  />
                </el-form-item>
                <el-form-item
                  :label="$t('附件上传：')"
                  prop="case_erp_supplierrisk_f_SupplierFile"
                >
                  <l-upload
                    drag
                    maxSize="10"
                    sizeType="MB"
                    :limit="3"
                    showFileList="uploadTheme1"
                    :disabled="
                      !lr_formEditAuth(
                        'case_erp_supplierrisk_f_SupplierFile',
                        false || isReadOnly
                      )
                    "
                    v-model="yearFormData.case_erp_supplierrisk_f_SupplierFile"
                    :placeholder="$t('请输入')"
                  />
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="24">
              <div class="learun-form-title mt-16">
                {{ $t("环境与安全评估") }}
              </div>

              <div class="form_block">
                <el-form-item
                  :label="$t('等级：')"
                  prop="case_erp_supplierrisk_f_SafetyLevel"
                  v-if="
                    !lr_formEditAuth(
                      'case_erp_supplierrisk_f_SafetyLevel',
                      false || isReadOnly
                    )
                  "
                >
                  <el-tag
                    v-if="
                      yearFormData.case_erp_supplierrisk_f_SafetyLevel == '0'
                    "
                    >{{ $t("优秀") }}</el-tag
                  >
                  <el-tag
                    v-else-if="
                      yearFormData.case_erp_supplierrisk_f_SafetyLevel == '1'
                    "
                    type="success"
                    >{{ $t("良好") }}</el-tag
                  >
                  <el-tag
                    v-else-if="
                      yearFormData.case_erp_supplierrisk_f_SafetyLevel == '2'
                    "
                    type="info"
                    >{{ $t("及格") }}</el-tag
                  >
                  <el-tag v-else type="danger">{{ $t("不及格") }}</el-tag>
                </el-form-item>
                <el-form-item
                  :label="$t('等级选择：')"
                  v-else
                  prop="case_erp_supplierrisk_f_SafetyLevel"
                >
                  <l-radio
                    :options="levelOptions"
                    v-model="yearFormData.case_erp_supplierrisk_f_SafetyLevel"
                  />
                </el-form-item>
                <el-form-item
                  :label="$t('评估理由：')"
                  prop="case_erp_supplierrisk_f_SafetyReason"
                >
                  <div v-if="isReadOnly">
                    {{ yearFormData.case_erp_supplierrisk_f_SafetyReason }}
                  </div>
                  <el-input
                    v-else
                    type="textarea"
                    style="width: 500px"
                    :disabled="
                      !lr_formEditAuth(
                        'case_erp_supplierrisk_f_SafetyReason',
                        isReadOnly
                      )
                    "
                    v-model="yearFormData.case_erp_supplierrisk_f_SafetyReason"
                    :placeholder="$t('请输入')"
                  />
                </el-form-item>
                <el-form-item
                  :label="$t('附件上传：')"
                  prop="case_erp_supplierrisk_f_SafetyFile"
                >
                  <l-upload
                    drag
                    maxSize="10"
                    sizeType="MB"
                    :limit="3"
                    showFileList="uploadTheme1"
                    :disabled="
                      !lr_formEditAuth(
                        'case_erp_supplierrisk_f_SafetyFile',
                        false || isReadOnly
                      )
                    "
                    v-model="yearFormData.case_erp_supplierrisk_f_SafetyFile"
                    :placeholder="$t('请输入')"
                  />
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="24">
              <div class="learun-form-title mt-16">
                {{ $t("最终评估结果") }}
              </div>
              <div class="form_block">
                <el-form-item
                  :label="$t('等级：')"
                  prop="case_erp_supplierrisk_f_FinalState"
                  v-if="
                    !lr_formEditAuth(
                      'case_erp_supplierrisk_f_FinalState',
                      false || isReadOnly
                    )
                  "
                >
                  <el-tag
                    v-if="
                      yearFormData.case_erp_supplierrisk_f_FinalState == '0'
                    "
                  >
                    <i class="el-icon-s-check margin_right_3"></i
                    >{{ $t("优秀") }}
                  </el-tag>
                  <el-tag
                    v-else-if="
                      yearFormData.case_erp_supplierrisk_f_FinalState == '1'
                    "
                    type="success"
                  >
                    <i class="el-icon-s-check margin_right_3"></i>
                    {{ $t("良好") }}
                  </el-tag>
                  <el-tag
                    v-else-if="
                      yearFormData.case_erp_supplierrisk_f_FinalState == '2'
                    "
                    type="info"
                  >
                    <i class="el-icon-s-check margin_right_3"></i>
                    {{ $t("及格") }}
                  </el-tag>
                  <el-tag v-else type="danger">
                    <i class="el-icon-s-check margin_right_3"></i>
                    {{ $t("不及格") }}
                  </el-tag>
                </el-form-item>
                <el-form-item
                  :label="$t('等级选择：')"
                  v-else
                  prop="case_erp_supplierrisk_f_FinalState"
                >
                  <l-radio
                    :options="levelOptions"
                    v-model="yearFormData.case_erp_supplierrisk_f_FinalState"
                  />
                </el-form-item>
                <el-form-item
                  :label="$t('评估理由：')"
                  prop="case_erp_supplierrisk_f_FinalReason"
                >
                  <div v-if="isReadOnly">
                    {{ yearFormData.case_erp_supplierrisk_f_FinalReason }}
                  </div>
                  <el-input
                    v-else
                    type="textarea"
                    style="width: 500px"
                    :disabled="
                      !lr_formEditAuth(
                        'case_erp_supplierrisk_f_FinalReason',
                        isReadOnly
                      )
                    "
                    v-model="yearFormData.case_erp_supplierrisk_f_FinalReason"
                    :placeholder="$t('请输入')"
                  />
                </el-form-item>
              </div>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-row>
  </div>
</template>
<script>
const api = window.$api.erpcase.supplier;
export default {
  props: {
    isReadOnly: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      moduleCode: "ERP_FormalSuppliers",
      formData: {
        case_erp_supplier_f_Number: "", // 供应商编号
        case_erp_supplier_f_Name: "", // 供应商名称
        case_erp_supplier_f_Person: "", // 供应商负责人
        case_erp_supplier_f_Type: "", // 供应商类型
        case_erp_supplier_f_CreateUserName: "", // 添加人
        case_erp_supplier_f_CreateDate: "", // 创建日期
        case_erp_supplier_f_Scope: "", // 经营范围
        case_erp_supplier_f_Phone: "", // 手机号
      },
      yearFormData: {
        case_erp_supplierrisk_f_CapacityLevel: "", // 等级选择：
        case_erp_supplierrisk_f_CapacityReason: "", // 评估理由：
        case_erp_supplierrisk_f_CapacityFile: "", // 附件上传：
        case_erp_supplierrisk_f_SupplierLevel: "", // 等级选择：
        case_erp_supplierrisk_f_SupplierReason: "", // 评估理由：
        case_erp_supplierrisk_f_SupplierFile: "", // 附件上传：
        case_erp_supplierrisk_f_SafetyLevel: "", // 等级选择：
        case_erp_supplierrisk_f_SafetyReason: "", // 评估理由：
        case_erp_supplierrisk_f_SafetyFile: "", // 附件上传：
        case_erp_supplierrisk_f_FinalState: "", // 等级选择：
        case_erp_supplierrisk_f_FinalReason: "", // 评估理由：
      },
      rules: {
        case_erp_supplierrisk_f_CapacityLevel: [
          { required: true, message: "请选择：" },
        ],
        case_erp_supplierrisk_f_CapacityReason: [
          { required: true, message: "请输入：" },
        ],
        case_erp_supplierrisk_f_SupplierLevel: [
          { required: true, message: "请选择：" },
        ],
        case_erp_supplierrisk_f_SupplierReason: [
          { required: true, message: "请输入：" },
        ],
        case_erp_supplierrisk_f_SafetyLevel: [
          { required: true, message: "请选择：" },
        ],
        case_erp_supplierrisk_f_SafetyReason: [
          { required: true, message: "请输入：" },
        ],
        case_erp_supplierrisk_f_FinalState: [
          { required: true, message: "请选择：" },
        ],
        case_erp_supplierrisk_f_FinalReason: [
          { required: true, message: "请输入：" },
        ],
      },

      id: "",
    };
  },
  computed: {
    levelOptions() {
      return [
        { label: "优秀", value: "0" },
        { value: "1", label: "良好" },
        { value: "2", label: "及格" },
        { value: "3", label: "不及格" },
      ];
    },
    yearName(){
      if(this.yearFormData.case_erp_supplierrisk_f_CreateDate){
        return this.lr_dateFormat(this.yearFormData.case_erp_supplierrisk_f_CreateDate,'yyyy');
      }
      return this.$getDayTime("yyyy");
    }
  },
  methods: {
    // 流程发起初始化表单调用
    async init(wfParams) {
      if (wfParams) {
        const { id } = wfParams;
        if (id) {
          this.id = id;
        }
        await this.loadSupplierInfo(this.id);
      }
      return false; // 表示当前表单是否是更新状态
    },
    // 加载供应商信息
    async loadSupplierInfo(id) {
      const supplierInfo = await this.$awaitWraper(api.get(id));
      if (supplierInfo) {
        supplierInfo.f_CreateDate = this.$formatDate(
          supplierInfo.f_CreateDate,
          "yyyy-MM-dd"
        );
        this.$setFormData(this.formData, supplierInfo, "case_erp_supplier");
      }
    },

    // 重置表单
    resetForm() {
      this.$formClear(this.$refs.form);
    },
    // 校验表单
    async validateForm() {
      if (!(await this.$formValidateWraper(this.$refs.form))) {
        return false;
      }
      return true;
    },

    // 设置表单数据
    setForm(data) {
      this.$setFormData(this.yearFormData, data, "case_erp_supplierrisk");
    },
    // 获取表单数据
    getForm() {
      const formData = this.$getFormData(
        this.yearFormData,
        "case_erp_supplierrisk"
      );
      if (this.id) {
        formData.f_Type = 1;
        formData.f_SupplierId = this.id;
      }
      return formData;
    },

    async loadFormData({ keyValue /*,params,node*/ }) {
      //  keyValue 流程中相当于流程processId,params 传递参数,node 流程节点
      const yearInfo = await this.$awaitWraper(api.getYear(keyValue));
      if (yearInfo) {
        this.id = yearInfo.f_SupplierId;
        await this.loadSupplierInfo(yearInfo.f_SupplierId);
        this.setForm(yearInfo);
        return true;
      } else {
        return false;
      }
    },

    async saveForm({ keyValue, isEdit /*,code,node*/ }) {
      // isEdit 是否更新数据, keyValue 流程中相当于流程processId,code 表示流程中的操作码,node 流程节点
      const postData = this.getForm();
      if (isEdit) {
        // 编辑
        return true;
      } else {
        postData.f_Id = keyValue;
        // 新增
        return await this.$awaitWraper(api.addAssess(postData));
      }
    },
  },
};
</script>